package com.woniuxy.school.dietary.food.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniuxy.school.dietary.food.entity.FoodInfo;
import com.woniuxy.school.dietary.food.entity.ProductsFoodRel;
import com.woniuxy.school.dietary.food.entity.ProductsInfo;
import com.woniuxy.school.dietary.food.mapper.FoodInfoMapper;
import com.woniuxy.school.dietary.food.mapper.ProductsFoodRelMapper;
import com.woniuxy.school.dietary.food.mapper.ProductsInfoMapper;
import com.woniuxy.school.dietary.food.model.from.AddProductsInfoForm;
import com.woniuxy.school.dietary.food.service.ProductsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年05月14日
 */
@Service
public class ProductsInfoServiceImpl extends ServiceImpl<ProductsInfoMapper, ProductsInfo> implements ProductsInfoService {
@Autowired
private ProductsInfoMapper productsInfoMapper;
@Autowired
private ProductsFoodRelMapper productsFoodRelMapper;
@Autowired
private FoodInfoMapper foodInfoMapper;

    @Override
    public void addProductsInfo(AddProductsInfoForm addProductsInfoForm, String userId) {
        //  判断菜品是否存在
        LambdaQueryWrapper<FoodInfo> pro2 = new LambdaQueryWrapper<>();
        pro2.eq(FoodInfo::getId, addProductsInfoForm.getFoodId()).eq(FoodInfo::getStoreId, userId);
        FoodInfo foodInfo = foodInfoMapper.selectOne(pro2);
        if(foodInfo == null){
            throw new RuntimeException("该菜品不存在");
        }
        //  添加餐品类别
        ProductsInfo productsInfo = new ProductsInfo();
        LambdaQueryWrapper<ProductsInfo> pro = new LambdaQueryWrapper<>();
        pro.eq(ProductsInfo::getProductsName, addProductsInfoForm.getProductsName());
        //  判断该类别是否存在
        if (productsInfoMapper.selectOne(pro) == null) {
            Snowflake snowflake = new Snowflake(1, 1);
            productsInfo.setId(snowflake.nextId());
            productsInfo.setProductsName(addProductsInfoForm.getProductsName());
            productsInfo.setProductsIntr(addProductsInfoForm.getProductsIntr());
            productsInfoMapper.insert(productsInfo);
        }
        //  添加餐品类别关系
        ProductsFoodRel  productsFoodRel = new ProductsFoodRel();
        LambdaQueryWrapper<ProductsInfo> pro1 = new LambdaQueryWrapper<>();
        pro1.eq(ProductsInfo::getProductsName, addProductsInfoForm.getProductsName());
        ProductsInfo productsInfo1 = productsInfoMapper.selectOne(pro1);
        Snowflake snowflake = new Snowflake(1, 1);
        productsFoodRel.setId(snowflake.nextId());
        productsFoodRel.setProductsId(productsInfo1.getId());
        productsFoodRel.setFoodId(addProductsInfoForm.getFoodId());
        productsFoodRel.setCreateDate(LocalDateTime.now());
        productsFoodRelMapper.insert(productsFoodRel);
    }

    @Override
    public List<ProductsInfo> findProductsInfoByStoreId(Long storeId) {
        //  查询该商家的菜品
        LambdaQueryWrapper<FoodInfo> foodInfo = new LambdaQueryWrapper<>();
        foodInfo.eq(FoodInfo::getStoreId, storeId);
        List<FoodInfo> foodInfoList = foodInfoMapper.selectList(foodInfo);
        Set<ProductsInfo> productsInfoSet = new HashSet<>();
        if(foodInfoList != null){
            for (FoodInfo foodInfo1 : foodInfoList) {
                // 查询该商家的餐品类别id
                LambdaQueryWrapper<ProductsFoodRel> productsFoodRel = new LambdaQueryWrapper<>();
                productsFoodRel.eq(ProductsFoodRel::getFoodId, foodInfo1.getId());
                ProductsFoodRel productsFoodRel1 = productsFoodRelMapper.selectOne(productsFoodRel);
                // 查询该商家的餐品类别
                LambdaQueryWrapper<ProductsInfo> productsInfo = new LambdaQueryWrapper<>();
                productsInfo.eq(ProductsInfo::getId, productsFoodRel1.getProductsId());
                ProductsInfo productsInfo1 = productsInfoMapper.selectOne(productsInfo);
                //  添加餐品类别
                productsInfoSet.add(productsInfo1);
            }
        }
        return (List<ProductsInfo>) productsInfoSet;
    }
}
